package com.jml.jdk8.optional;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author:jml
 * @date: 2019/7/31 0031 22:20
 * @description：
 */
public class OptionalTest1 {

    public static void main(String[] args) {
        Employee employee = new Employee();
        employee.setName("zhangsan");

        Employee employee2 = new Employee();
        employee2.setName("lisi");

        Company company = new Company();
        company.setName("company1");
        List<Employee> employees =Arrays.asList(employee,employee2);
        company.setEmployees(employees);
        System.out.println(employees);

        //得到List，list为null就会新的空list，不为null就返回原值
        List<Employee> employees1 = Optional.ofNullable(company.getEmployees()).orElse(new ArrayList<Employee>());
        System.out.println(employees1);

        //map方法就是先执行function接口x -> x.getEmployees()，参数x就是optional，结果在作为一个新的Optional返回去
        Optional<Company> optional = Optional.ofNullable(company);
        List<Employee> employees2 = optional.map(x -> x.getEmployees()).get();
        System.out.println(employees2);
    }


}
